package com.nbjtjc.safe.service.edu;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.*;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.model.edu.EduRequirementInvestigation;
import com.nbjtjc.safe.model.edu.EduRequirementRespondent;
import com.nbjtjc.safe.model.edu.EduRequirementSurveyItem;
import com.nbjtjc.safe.model.edu.EduRequirementUserSurveyResults;
import com.nbjtjc.safe.util.MyShiroUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EduRequirementInvestigationService {
	@Autowired
    private EduRequirementInvestigationMapper eduRequirementInvestigationMapper;
    @Autowired
    private EduRequirementSurveyItemMapper eduRequirementSurveyItemMapper;
    @Autowired
    private EduRequirementUserSurveyResultsMapper eduRequirementUserSurveyResultsMapper;
    @Autowired
    EduRequirementItemsSurveyResultsMapper eduRequirementItemsSurveyResultsMapper;
    @Autowired
    EduRequirementRespondentMapper eduRequirementRespondentMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    
    //非固定模板情况下的新建需求调查
    @ApiOperation(value="新增或更新需求调查", notes="根据传入的需求调查对象新增或更新")
    @Transactional(rollbackFor = {Throwable.class})
    public Result add(EduRequirementInvestigation eduRequirementInvestigation){
        Result result=new Result();
        if(ObjectUtil.isNull(eduRequirementInvestigation)
                ){
            result.setFalse("需求调查为空");
            return result;
        }
        //无主键或主键原表不存在则新建，插入数据
        if(ObjectUtil.isNull(eduRequirementInvestigation.getId())
        		|| ObjectUtil.isNull(eduRequirementInvestigationMapper.selectByPrimaryKey(eduRequirementInvestigation.getId()))){
        	
	        //同样年份一直能有一个
	        if(ObjectUtil.isNotNull(eduRequirementInvestigation.getCreateYear())){
//	        	EduRequirementInvestigation eduRequirementInvestigationYear=new EduRequirementInvestigation();
//	        	eduRequirementInvestigationYear.setCreateYear(eduRequirementInvestigation.getCreateYear());
//	        	eduRequirementInvestigationYear.setCompanyId(MyShiroUtils.getCompanyId());
//	        	if(CollectionUtil.isNotEmpty(eduRequirementInvestigationMapper.search(eduRequirementInvestigationYear))){
//		        	result.setFalse("该年份已有需求调查记录，不能重复增加");
//		            return result;
//	        	}
	        }else{
	        	result.setFalse("年份不能为空");
	            return result;
	        }
        	
	       	//记录当前用户单位作为预算计划公司
        	int  companeyId= MyShiroUtils.getCompanyId();
        	eduRequirementInvestigation.setCompanyId(companeyId);
        	eduRequirementInvestigation.setCompanyName(sysDeptMapper.findNameById(companeyId));
        	eduRequirementInvestigation.setCreateDate(new Date());
        	eduRequirementInvestigation.setCreateId(MyShiroUtils.getSessionUserId());
        	int res=eduRequirementInvestigationMapper.insertSelective(eduRequirementInvestigation);
	        if(res==0){
	            result.setFalse("插入失败");
	            return result;

	        }
	        //新建参加人员表
	    	if(CollectionUtil.isNotEmpty(eduRequirementInvestigation.getEduRequirementRespondents())){
		    	result = insertEduRequirementRespondent(eduRequirementInvestigation);
		    	if(result.isSuccess()==false){
		            return result;
		        }
	    	}
//	        //为测试用，如果没录项目，则按模板内容生成需求调查项目
//	        if(eduRequirementInvestigation.getEduRequirementSurveyItems().isEmpty()){
//	        	 res = eduRequirementSurveyItemMapper.insertTemplet(eduRequirementInvestigation.getId());
//	        	if(res==0){
//		            result.setFalse("插入模板内容失败");
//		            return result;
//		        }
//	        }else{
	        //如果项目内容已录入，则插入需求调查项目
		        result = insertEduRequirementSurveyItem(eduRequirementInvestigation);
		        if(result.isSuccess()==false){
		            return result;
		        }
//	        }
//	    	  //如果状态为已发布则创建用户需求调查列表
//	        if(eduRequirementInvestigation.getStatus()==0){
//		        //为每个用户创建需求调查结果列表
//		    	res = eduRequirementUserSurveyResultsMapper.createUserResult(eduRequirementInvestigation.getId());
//		    	if(res==0){
//		            result.setFalse("插入用户需求调查列表失败");
//		            return result;
//		        }
//		    	//为每个用户创建需求调查结果中的项目结果列表
//		    	res = eduRequirementItemsSurveyResultsMapper.createItemsResult(eduRequirementInvestigation.getId());
//		    	if(res==0){
//		            result.setFalse("插入用户需求调查项目内容失败");
//		            return result;
//		        }
//	        }

        }else
        {
//	        	else{
//	        		result.setFalse("调查项目不能为空，更新失败");
//	        		return result;
//	        	}
        	eduRequirementInvestigation.setModifyDate(new Date());
        	eduRequirementInvestigation.setModifyId(MyShiroUtils.getSessionUserId());
	          int res=eduRequirementInvestigationMapper.updateByPrimaryKeySelective(eduRequirementInvestigation);
	          if(res==0){
	        	  result.setFalse("更新失败");
		          return result;
	          }
	          //更新人员列表
	          if(CollectionUtil.isNotEmpty(eduRequirementRespondentMapper.findByReqId(eduRequirementInvestigation.getId()))){
		          eduRequirementRespondentMapper.deleteByReqId(eduRequirementInvestigation.getId());
	          }
	      	if(CollectionUtil.isNotEmpty(eduRequirementInvestigation.getEduRequirementRespondents())){
	          result = insertEduRequirementRespondent(eduRequirementInvestigation);
		    	if(result.isSuccess()==false){
		            return result;
		        }
	      	}
	          //调查项目正常情况不会为空，为方便测试时不用输项目内容，为空则不更新调查项目，实际为空将更新失败。
        	  if(eduRequirementInvestigation.getEduRequirementSurveyItems().isEmpty()==false){
        		  res = eduRequirementSurveyItemMapper.deleteByReqInvestId(eduRequirementInvestigation.getId());
		    	  if(res==0){
		              result.setFalse("插入用户需求调查项目内容失败");
		              return result;
		          }
		      	  result = insertEduRequirementSurveyItem(eduRequirementInvestigation);
		    	  if(result.isSuccess()==false){
		              return result;
		          }
//		    	  //如果状态为已发布则创建用户需求调查列表
//			        if(eduRequirementInvestigation.getStatus()==1){
//				        //为每个用户创建需求调查结果列表
//				    	res = eduRequirementUserSurveyResultsMapper.createUserResult(eduRequirementInvestigation.getId());
//				    	if(res==0){
//				            result.setFalse("插入用户需求调查列表失败");
//				            return result;
//				        }
//				    	//为每个用户创建需求调查结果中的项目结果列表
//				    	res = eduRequirementItemsSurveyResultsMapper.createItemsResult(eduRequirementInvestigation.getId());
//				    	if(res==0){
//				            result.setFalse("插入用户需求调查项目内容失败");
//				            return result;
//				        }
//			        }
        	  }
        	
        }
        
        result.setData(eduRequirementInvestigation);
        return result;
    }
    
    private Result insertEduRequirementSurveyItem(EduRequirementInvestigation eduRequirementInvestigation){
        Result result=new Result();
    	if(CollectionUtil.isEmpty(eduRequirementInvestigation.getEduRequirementSurveyItems())){
            result.setFalse("插入对象为空");
        	return result;

	    }
    	//将RequirementSurveyItem的reqInvestId都设为当前eduRequirementInvestigation的ID
    	List<EduRequirementSurveyItem> eduRequirementSurveyItems =  eduRequirementInvestigation.getEduRequirementSurveyItems();
		for(EduRequirementSurveyItem eduRequirementSurveyItem: eduRequirementSurveyItems){
			eduRequirementSurveyItem.setReqInvestId(eduRequirementInvestigation.getId());
		}
		
    	int res=eduRequirementSurveyItemMapper.insertList(eduRequirementInvestigation.getEduRequirementSurveyItems());
    	if(res==0){
            result.setFalse("插入需求调查项目失败");
            return result;
        }
  	  //如果状态为已发布则创建用户需求调查列表
        if(eduRequirementInvestigation.getStatus()==1){
	        //为每个用户创建需求调查结果列表
	    	res = eduRequirementUserSurveyResultsMapper.createUserResult(eduRequirementInvestigation.getId());
	    	if(res==0){
	            result.setFalse("插入用户需求调查列表失败");
	            return result;
	        }
	    	//为每个用户创建需求调查结果中的项目结果列表
	    	res = eduRequirementItemsSurveyResultsMapper.createItemsResult(eduRequirementInvestigation.getId());
	    	if(res==0){
	            result.setFalse("插入用户需求调查项目内容失败");
	            return result;
	        }
        }
    	result.setData(eduRequirementInvestigation.getEduRequirementSurveyItems());
    	return result;
    }


    private Result insertEduRequirementRespondent(EduRequirementInvestigation eduRequirementInvestigation){
        Result result=new Result();
    	if(CollectionUtil.isEmpty(eduRequirementInvestigation.getEduRequirementRespondents())){
            result.setFalse("需求调查对象用户插入对象为空");
        	return result;

	    }
    	//将getEduRequirementRespondents的req_invest_id都设为当前eduRequirementInvestigation的ID
    	List<EduRequirementRespondent> eduRequirementRespondents =  eduRequirementInvestigation.getEduRequirementRespondents();
		for(int i = 0;i<eduRequirementRespondents.size();i++){
			eduRequirementRespondents.get(i).setReqInvestId(eduRequirementInvestigation.getId());
			eduRequirementRespondents.get(i).setCancel(0);

		}
//    	for(EduRequirementRespondent eduRequirementRespondent: eduRequirementRespondents){
//			eduRequirementRespondent.setReqInvestId(eduRequirementInvestigation.getId());
//			eduRequirementRespondent.setCancel(0);
//		}
		
    	int res = eduRequirementRespondentMapper.insertList(eduRequirementRespondents);
    	if(res==0){
            result.setFalse("新建需求调查对象用户列表失败");
            return result;
        }
    	result.setData(eduRequirementRespondents);
    	return result;
    }
//    private Result updateEduRequirementSurveyItem(EduRequirementInvestigation eduRequirementInvestigation){
//        Result result=new Result();
//    	if(CollectionUtil.isEmpty(eduRequirementInvestigation.getEduRequirementSurveyItems())){
//            result.setFalse("更新对象为空");
//        	return result;
//	    }
//    	ArrayList<Integer> eduRequirementSurveyItemsIdDataBase = eduRequirementSurveyItemMapper.findIdByReqInvestId(eduRequirementInvestigation.getId());
//    	ArrayList<Integer> eduRequirementSurveyItemsId = new ArrayList();
//    	//将eduRequirementSurveyItems的reqInvestId都设为当前eduRequirementInvestigation的ID
//    	//若库中已有该项目（ID不为空，库中查得到）则更新，否则（该项目ID为空，或库中查不到）插入
//    	List<EduRequirementSurveyItem> eduRequirementSurveyItems =  eduRequirementInvestigation.getEduRequirementSurveyItems();
//    	
//		for(EduRequirementSurveyItem eduRequirementSurveyItem: eduRequirementSurveyItems){
//			//项目为空则跳过，继续下一个
//			if(ObjectUtil.isNull(eduRequirementSurveyItem)){
//	            continue;
//			}
//			//如果ID不为空，加入eduRequirementSurveyItemsId
//			if(ObjectUtil.isNotNull(eduRequirementSurveyItem.getId())){
//				eduRequirementSurveyItemsId.add(eduRequirementSurveyItem.getId());
//			}
//			int res=0;
//	    	//将eduRequirementSurveyItem的reqInvestId设为当前eduRequirementInvestigation的ID
//			eduRequirementSurveyItem.setReqInvestId(eduRequirementInvestigation.getId());;
//			//库中已有该项目（ID不为空，库中查得到）则更新
//			if(ObjectUtil.isNotNull(eduRequirementSurveyItem.getId()) && ObjectUtil.isNotNull(eduRequirementSurveyItemMapper.selectByPrimaryKey(eduRequirementSurveyItem.getId()))){
//				res = eduRequirementSurveyItemMapper.updateByPrimaryKeySelective(eduRequirementSurveyItem);
//		    	if(res==0){
//		            result.setFalse("更新需求调查项目失败");
//		            return result;
//		        }
//			}else{
//				//（该项目ID为空，或库中查不到）插入
//		    	res=eduRequirementSurveyItemMapper.insertSelective(eduRequirementSurveyItem);
//		    	if(res==0){
//		            result.setFalse("插入需求调查项目失败");
//		            return result;
//		        }
//		    	//创建用户需求调查
//		    	
//			}
//		}
//		//库中有而更新内容没有的项目的ID，是要删除的项目
//		for(Integer id:eduRequirementSurveyItemsIdDataBase){
//			int res=-1;
//			if(eduRequirementSurveyItemsId.contains(id)==false){
//				res = eduRequirementSurveyItemMapper.deleteByPrimaryKey(id);
//		    	if(res==0){
//		            result.setFalse("删除库中需求调查项目失败");
//		            return result;
//		        }
//			}
//
//		}
//    	result.setData(eduRequirementSurveyItems);
//    	return result;
//    }

    public Result delete(Integer id){
        Result result=new Result();
        if(ObjectUtil.isNull(id)){
            result.setFalse("删除对象不能为空");
            return result;
        }
//        int res = eduRequirementInvestigationMapper.deleteByPrimaryKey(id);
        int res = eduRequirementInvestigationMapper.cancelByPrimaryKey(id);
     	if(res==0){
            result.setFalse("删除对象失败");
            return result;
        }
        return result;
    }
    
	public Result  show(EduRequirementInvestigation eduRequirementInvestigation){
		Result result = new Result();
		//update 如何判断成功
//		if(ObjectUtil.isNull(reqInvestId))
//		{
//            result.setFalse("主键不存在");
//            return result;
//		}
		if(ObjectUtil.isNull(eduRequirementInvestigation)||ObjectUtil.isNull(eduRequirementInvestigation.getId())){
          result.setFalse("对象不能为空,主键不能为空");
          return result;
		}
		//更新需求调查汇总结果数
//		int res = eduRequirementSurveyItemMapper.updateCountItemsByReqInvestId(eduRequirementInvestigation.getId());
//	 	if(res==0){
//            result.setFalse("更新调查结果失败");
//            return result;
//        }
		EduRequirementInvestigation eduReq = eduRequirementInvestigationMapper.findDetailById(eduRequirementInvestigation.getId()); 
//		if(ObjectUtil.isNull(eduRequirementInvestigations))
//		{
//            result.setFalse("查询对象不存在");
//            return result;
//		}
		result.setData(eduReq);

		return result;
	}
	public Result find(EduRequirementInvestigation eduRequirementInvestigation) {
		Result result = new Result();
		PageHelper.startPage(eduRequirementInvestigation.getPageCurrent(), eduRequirementInvestigation.getPageSize());


		List<EduRequirementInvestigation> eduRequirementInvestigations = eduRequirementInvestigationMapper.search(eduRequirementInvestigation);

		result.setData(new PageInfo<>(eduRequirementInvestigations));
		return result;
	}
	
	public Result findDetail(EduRequirementInvestigation eduRequirementInvestigation) {
		Result result = new Result();
		PageHelper.startPage(eduRequirementInvestigation.getPageCurrent(), eduRequirementInvestigation.getPageSize());


		List<EduRequirementInvestigation> eduRequirementInvestigations = eduRequirementInvestigationMapper.searchDetail(eduRequirementInvestigation);

		result.setData(new PageInfo<>(eduRequirementInvestigations));
		return result;
	}
	
	  public Result findSubcompany(EduRequirementInvestigation eduRequirementInvestigation) {
	      Result result = new Result();
	      PageHelper.startPage(eduRequirementInvestigation.getPageCurrent(), eduRequirementInvestigation.getPageSize());
	      if(ObjectUtil.isNull(eduRequirementInvestigation.getCompanyId())){
	    	  result.setFalse("缺少公司ID，无法据此查看下级公司需求调查");
	    	  return result;
	      }
	      	List<EduRequirementInvestigation> eduRequirementInvestigations = eduRequirementInvestigationMapper.searchSubcompany(eduRequirementInvestigation.getCompanyId());
	      result.setData(new PageInfo<>(eduRequirementInvestigations));

	      return result;
	  }
	  
		public Result manegerFindDetail(EduRequirementInvestigation eduRequirementInvestigation) {
			Result result = new Result();
			PageHelper.startPage(eduRequirementInvestigation.getPageCurrent(), eduRequirementInvestigation.getPageSize());


			List<EduRequirementInvestigation> eduRequirementInvestigations = eduRequirementInvestigationMapper.searchDetail(eduRequirementInvestigation);

			result.setData(new PageInfo<>(eduRequirementInvestigations));
			return result;
		}

	
	
	//汇总已填报需求调查的总人数
	public Result totalRespondent(int id) {
		Result result = new Result();
		int t = eduRequirementItemsSurveyResultsMapper.totalRespondent(id);
		result.setData(t);
		return result;
	}
	
	//延期重启
	public Result restart(EduRequirementInvestigation eduRequirementInvestigation) {
		Result result = new Result();
		if(ObjectUtil.isNull(eduRequirementInvestigation)
				||ObjectUtil.isNull(eduRequirementInvestigation.getId())){
			result.setFalse("需求调查和ID不能为空");
			return result;
		}
		int id = eduRequirementInvestigation.getId();
		eduRequirementInvestigation = eduRequirementInvestigationMapper.selectByPrimaryKey(id);
		if(ObjectUtil.isNull(eduRequirementInvestigation.getStatus())){
			result.setFalse("状态为空");
			return result;
		}
		if(eduRequirementInvestigation.getStatus()!=2){
			result.setFalse("不是已完成状态");
			return result;
		}
		//状态设置为进行中
		eduRequirementInvestigation.setStatus(1);
		int res = eduRequirementInvestigationMapper.updateByPrimaryKeySelective(eduRequirementInvestigation);
		if(res == 0 ){
			result.setFalse("更新失败");
			return result;
		}
		//记录此时未填人员 
		EduRequirementUserSurveyResults eduRequirementUserSurveyResultsTemp= new EduRequirementUserSurveyResults();
		List<EduRequirementUserSurveyResults> eduRequirementUserSurveyResultss = eduRequirementUserSurveyResultsMapper.findUndoUserByReqId(id,0);
		List<Integer> userIds = new ArrayList();
		if(CollectionUtil.isNotEmpty(eduRequirementUserSurveyResultss)){
			for(int i=0;i<eduRequirementUserSurveyResultss.size();i++){
				if(ObjectUtil.isNotNull(eduRequirementUserSurveyResultss.get(i).getUserId())){
					userIds.add(i, eduRequirementUserSurveyResultss.get(i).getUserId());
				}
			}
		}else{
			userIds=null;
		}

		
		
		result.setData(userIds);
		return result;
	}
	//模块固定情况下的新建需求调查
	@ApiOperation(value="新增或更新需求调查", notes="根据传入的需求调查对象新增或更新")
    @Transactional(rollbackFor = {Throwable.class})
    public Result addByTemplet(EduRequirementInvestigation eduRequirementInvestigation){
        Result result=new Result();
        if(ObjectUtil.isNull(eduRequirementInvestigation)
                ){
            result.setFalse("需求调查为空");
            return result;
        }
        //无主键或主键原表不存在则新建，插入数据，直接提交
        if(ObjectUtil.isNull(eduRequirementInvestigation.getId())
        		|| ObjectUtil.isNull(eduRequirementInvestigationMapper.selectByPrimaryKey(eduRequirementInvestigation.getId()))){
        	
	        //同样年份一直能有一个
	        if(ObjectUtil.isNotNull(eduRequirementInvestigation.getCreateYear())){
	        	EduRequirementInvestigation eduRequirementInvestigationYear=new EduRequirementInvestigation();
	        	eduRequirementInvestigationYear.setCreateYear(eduRequirementInvestigation.getCreateYear());
	        	eduRequirementInvestigationYear.setCompanyId(MyShiroUtils.getCompanyId());

	        	if(CollectionUtil.isNotEmpty(eduRequirementInvestigationMapper.search(eduRequirementInvestigationYear))){
		        	result.setFalse("该年份已有需求调查记录，不能重复增加");
		            return result;
	        	}
	        }else{
	        	result.setFalse("年份不能为空");
	            return result;
	        }
        	//记录当前用户单位作为预算计划公司
        	int  companeyId= MyShiroUtils.getCompanyId();
        	eduRequirementInvestigation.setCompanyId(companeyId);
        	eduRequirementInvestigation.setCompanyName(sysDeptMapper.findNameById(companeyId));
        	eduRequirementInvestigation.setCreateDate(new Date());
        	eduRequirementInvestigation.setCreateId(MyShiroUtils.getSessionUserId());
        	
        	int res=eduRequirementInvestigationMapper.insertSelective(eduRequirementInvestigation);
	        if(res==0){
	            result.setFalse("插入失败");
	            return result;

	        }
	        //新建参加人员表
	        int id1 = eduRequirementInvestigation.getId();
	    	if(CollectionUtil.isNotEmpty(eduRequirementInvestigation.getEduRequirementRespondents())){
		    	result = insertEduRequirementRespondent(eduRequirementInvestigation);
		    	if(result.isSuccess()==false){
		            return result;
		        }
	    	}

	        //根据模板新增项目
	        res = eduRequirementSurveyItemMapper.insertItemByTemplet(eduRequirementInvestigation.getId());
	        if(res==0){
	            result.setFalse("创建需求调查项目失败");
	            return result;
	        }
	        //为每个用户创建需求调查结果列表
	    	res = eduRequirementUserSurveyResultsMapper.createUserResult(eduRequirementInvestigation.getId());
	    	if(res==0){
	            result.setFalse("插入用户需求调查列表失败");
	            return result;
	        }
	    	//为每个用户创建需求调查结果中的项目结果列表
	    	res = eduRequirementItemsSurveyResultsMapper.createItemsResult(eduRequirementInvestigation.getId());
	    	if(res==0){
	            result.setFalse("插入用户需求调查项目内容失败");
	            return result;
	        }

        }else
        {
        	eduRequirementInvestigation.setModifyDate(new Date());
        	eduRequirementInvestigation.setModifyId(MyShiroUtils.getSessionUserId());
        	//更新提交
	          int res=eduRequirementInvestigationMapper.updateByPrimaryKeySelective(eduRequirementInvestigation);
	          if(res==0){
	        	  result.setFalse("更新失败");
		          return result;
	          }
	          //更新人员列表
	          if(CollectionUtil.isNotEmpty(eduRequirementRespondentMapper.findByReqId(eduRequirementInvestigation.getId()))){
		          eduRequirementRespondentMapper.deleteByReqId(eduRequirementInvestigation.getId());
	          }
	      	if(CollectionUtil.isNotEmpty(eduRequirementInvestigation.getEduRequirementRespondents())){
	          result = insertEduRequirementRespondent(eduRequirementInvestigation);
		    	if(result.isSuccess()==false){
		            return result;
		        }
	      	}
	      	
	        //根据模板新增项目
	        res = eduRequirementSurveyItemMapper.insertItemByTemplet(eduRequirementInvestigation.getId());
	        if(res==0){
	            result.setFalse("创建需求调查项目失败");
	            return result;
	        }
	        //为每个用户创建需求调查结果列表
	    	res = eduRequirementUserSurveyResultsMapper.createUserResult(eduRequirementInvestigation.getId());
	    	if(res==0){
	            result.setFalse("插入用户需求调查列表失败");
	            return result;
	        }
	    	//为每个用户创建需求调查结果中的项目结果列表
	    	res = eduRequirementItemsSurveyResultsMapper.createItemsResult(eduRequirementInvestigation.getId());
	    	if(res==0){
	            result.setFalse("插入用户需求调查项目内容失败");
	            return result;
	        }

        }
        
        result.setData(eduRequirementInvestigation);
        return result;
    }
	
	@ApiOperation(value="保存不提交需求调查", notes="根据传入的需求调查对象新增或更新")
    @Transactional(rollbackFor = {Throwable.class})
    public Result save(EduRequirementInvestigation eduRequirementInvestigation){
        Result result=new Result();
        if(ObjectUtil.isNull(eduRequirementInvestigation)
                ){
            result.setFalse("需求调查为空");
            return result;
        }
        //无主键或主键原表不存在则新建，插入数据
        if(ObjectUtil.isNull(eduRequirementInvestigation.getId())
        		|| ObjectUtil.isNull(eduRequirementInvestigationMapper.selectByPrimaryKey(eduRequirementInvestigation.getId()))){
        	
	        //同样年份一直能有一个
	        if(ObjectUtil.isNotNull(eduRequirementInvestigation.getCreateYear())){
	        	EduRequirementInvestigation eduRequirementInvestigationYear=new EduRequirementInvestigation();
	        	eduRequirementInvestigationYear.setCreateYear(eduRequirementInvestigation.getCreateYear());
	        	if(CollectionUtil.isNotEmpty(eduRequirementInvestigationMapper.search(eduRequirementInvestigationYear))){
		        	result.setFalse("该年份已有需求调查记录，不能重复增加");
		            return result;
	        	}
	        }else{
	        	result.setFalse("年份不能为空");
	            return result;
	        }
        	//记录当前用户单位作为预算计划公司
        	int  companeyId= MyShiroUtils.getCompanyId();
        	eduRequirementInvestigation.setCompanyId(companeyId);
        	eduRequirementInvestigation.setCompanyName(sysDeptMapper.findNameById(companeyId));
        	eduRequirementInvestigation.setCreateDate(new Date());
        	eduRequirementInvestigation.setCreateId(MyShiroUtils.getSessionUserId());
	        
        	int res=eduRequirementInvestigationMapper.insertSelective(eduRequirementInvestigation);
	        if(res==0){
	            result.setFalse("插入失败");
	            return result;
	        }
	        
	        int id1 = eduRequirementInvestigation.getId();
	        //新建参加人员表
	    	if(CollectionUtil.isNotEmpty(eduRequirementInvestigation.getEduRequirementRespondents())){
		    	result = insertEduRequirementRespondent(eduRequirementInvestigation);
		    	if(result.isSuccess()==false){
		            return result;
		        }
	    	}
        }else
        {
        	eduRequirementInvestigation.setModifyDate(new Date());
        	eduRequirementInvestigation.setModifyId(MyShiroUtils.getSessionUserId());
	          int res=eduRequirementInvestigationMapper.updateByPrimaryKeySelective(eduRequirementInvestigation);
	          if(res==0){
	        	  result.setFalse("更新失败");
		          return result;
	          }
	          
	          //更新人员列表
	          if(CollectionUtil.isNotEmpty(eduRequirementInvestigation.getEduRequirementRespondents())){
		          if(CollectionUtil.isNotEmpty(eduRequirementRespondentMapper.findByReqId(eduRequirementInvestigation.getId()))){
			          eduRequirementRespondentMapper.deleteByReqId(eduRequirementInvestigation.getId());
		          }
		          result = insertEduRequirementRespondent(eduRequirementInvestigation);
			    	if(result.isSuccess()==false){
			        	  result.setFalse("更新人员失败");
			            return result;
			        }
		      	
	          }


        }
        
        result.setData(eduRequirementInvestigation);
        return result;
    }
}
